--
-- Author: shunguo.chen
-- Date: 2017-12-28 11:20:28
--
-- PvpInfo	锦标赛信息

local Daibi = require("app.public.dao.Daibi")
local PvpSeasonInfo = import(".PvpSeasonInfo")
local PvpRewardVo = import(".PvpRewardVo")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
	self.seasonInfo = PvpSeasonInfo.new()

	if data then
		self:init(data)
	end

end

-- message TournamentInfo {
--    SeasonInfo seasonInfo = 1;//本赛季信息
--    int32 starCount = 2;
--    repeated TournamentBoostNum boostCount = 10; //剩余鼓励状态次数
--    repeated TournamentFirstWin firstWinList = 20;//赛事首胜列表
--    SeasonInfo lastSeasonInfo = 40; //上赛季信息，用于领奖
-- }
function ClassRef:init(data)
	self.seasonInfo:init(data.seasonInfo)
	self.starCount = UD:getArenaStarCount()

	self.boostCountList = {}
	for k,v in pairs(data.boostCount) do
		self.boostCountList[v.type] = v.boostNum
	end

	self.firstWinList = {}
	for k,v in pairs(data.firstWinList) do
		self.firstWinList[v.type] = v.hasFirstWon
	end

	-- 上赛季信息
	self.lastSeasonInfo = nil
	if data.lastSeasonInfo then
		self.lastSeasonInfo = PvpSeasonInfo.new(data.lastSeasonInfo)
	end

	self._starMax = UD:getLuckBoxLevelUpPoint(Enums.BoxType.pvp)

	self._initTime = app.session:getServerTime() or app:getTime()

	-- for test
	-- self.starCount = 20
	-- self.seasonInfo.lv = 16
	-- self.seasonInfo.score = 80
end

-- 返回赛季等级
function ClassRef:getSeasonLv()
	return self.seasonInfo.lv
end

-- 返回宝箱个数
function ClassRef:getBoxNum()
	return math.floor(self.starCount / self._starMax)
end

-- 和给的星数相比，宝箱数量是否和有改变
function ClassRef:isBoxNumChange(starCount)
	return self:getBoxNum() ~= (starCount / self._starMax)
end

-- 返回星星数
function ClassRef:getStar()
	return self.starCount % self._starMax
end

-- 缓存info变化前的数据
function ClassRef:setChangeCache(lv, score)
	if self._preLev then return end

	self._preLev = lv
	self._preScore = score
end

-- 有变化缓存数据
function ClassRef:hasChangeCache()
	return self._preLev ~= nil
end

-- 清除缓存
function ClassRef:clearChangeCache()
	self._preLev = nil
	self._preScore = nil
end

-- 获取变化所需展现数据，获取后会清除cache
function ClassRef:getChangeData()
	-- 星星变化数
	local starData = {UD:getStarCountCache(), UD:getArenaStarCount()}
	-- 等级／经验变化步骤，用于动画展现
	local steps = {}

	local curScore = self.seasonInfo.score
	local curLevel = self.seasonInfo.lv
	if  self._preScore and curScore ~= self._preScore then
		local level = self._preLev
		local conf = GD:queryPvpSeasonConf(level)
		local exp = self._preScore - tonumber(conf.totalExp)
		local maxExp = tonumber(conf.levelupExp)
		steps[#steps + 1] = {level=level, exp=exp, maxExp=maxExp}

		local factor = (curScore > self._preScore) and 1 or -1
		if level ~= curLevel then
			for i=level, curLevel+factor, -factor do
				if i ~= level then
					conf = GD:queryPvpSeasonConf(i)
					maxExp = tonumber(conf.levelupExp)
				end
				local curExp = (factor == 1) and maxExp or 0
				steps[#steps + 1] = {level=i, exp=curExp, maxExp=maxExp}
			end
		end

		if curLevel ~= level then
			conf = GD:queryPvpSeasonConf(curLevel)
		end

		level = curLevel
		exp = curScore - tonumber(conf.totalExp)
		maxExp = tonumber(conf.levelupExp)
		steps[#steps + 1] = {level=level, exp=exp, maxExp=maxExp}

		local len = #steps
		for i,step in ipairs(steps) do
			if i < len then
				local nextStep = steps[i+1]
				if factor == 1 then
					if step.exp ~= step.maxExp then
						step.nextExp = nextStep.exp
					else
						step.exp = 0
						step.level = nextStep.level
						step.nextExp = nextStep.exp
						step.maxExp = nextStep.maxExp
					end
				else
					if step.exp ~= 0 then
						step.nextExp = nextStep.exp
					else
						step.level = nextStep.level
						step.exp = nextStep.maxExp
						step.nextExp = nextStep.exp
						step.maxExp = nextStep.maxExp
					end
				end
			end
		end
	end

	-- clear cache
	self:clearChangeCache()
	UD:clearLuckBoxCacheByKey(Enums.lootType.arenaLuckBox)
	UD:clearLuckBoxCacheByKey(Enums.lootType.starCount)

	return starData, steps
end

-- 天切换的时候，更新一下次数数据
function ClassRef:dayChange()
	local boostCount = tonumber(GD:getSystemPar("tournamentBoostCount", 3))
	for k,v in pairs(self.boostCountList) do
		self.boostCountList[k] = boostCount
	end
	for k,v in pairs(self.firstWinList) do
		self.firstWinList[k] = false
	end
end

function ClassRef:isExpired()
	return not app.session:isToday(self._initTime)
end

-- 失败提示文字，id = 玩法id
function ClassRef:getFailStrs(id)
	local strs
	if self:isBoost(id) then
		-- str = L("tips_1150014")
		strs = {L("tips_1150014"), L("Nopunishment")}
	else
		local failExp = self.seasonInfo:getFailExp(id)
		if failExp > 0 then
			-- str = string.format(L("tips_1150015"), failExp)
			strs = {string.format(L("tips_1150015"), failExp)}
		else
			-- str = L("tips_1150016")
			strs = {L("tips_1150016"), L("Nopunishment")}
		end
	end
	return strs
end

-- 下一场胜利文本，id = 玩法id
function ClassRef:getNextStr(id)
	local failExp = self.seasonInfo:getFailExp(id)
	local str
	local colorChange = false
	if self:isBoost(id) then
		if failExp > 0 then
			str = L("tips_1150017")
		else
			str = L("tips_1150018")
		end
	else
		if failExp > 0 then
			colorChange = true
			str = string.format(L("tips_1150019"), failExp)
		else
			str = L("tips_1150020")
		end
	end
	return str, colorChange
end

-- 获取奖励, id = 玩法id
function ClassRef:getRewards(id)
	local isFirstWin = self:isFirstWin(id)
	local isBoost = self:isBoost(id)
	local rewards = {PvpRewardVo.new(), PvpRewardVo.new(), PvpRewardVo.new()}

	local conf = GD:queryChampionshipsConf( id )
	local item = UD:parseRewardToBagItem( conf.championReward1 )
	rewards[1].itemData = item

	item = UD:parseRewardToBagItem( conf.championReward2 )
	if isBoost then item:setAmount(item.amount*2) end
	rewards[2].itemData = item

	item = Daibi.new()
	item:read({tid=Constant.PVP_SESSION_EXP_ID, amount=self.seasonInfo:getWinExp(id)})
	if isBoost then item:setAmount(item.amount*2) end
	rewards[3].itemData = item

	for i,v in ipairs(rewards) do
		v.isFirstWin = isFirstWin
		v.isBoost = isBoost
	end
	return rewards
end

-- 玩法是否首胜
function ClassRef:isFirstWin(id)
	return not self.firstWinList[tonumber(id)]
end

-- 是否处于鼓励状态
function ClassRef:isBoost(id)
	return self.boostCountList[tonumber(id)] > 0
end

-- 获得鼓励次数
function ClassRef:getBoostCount(id)
	return self.boostCountList[tonumber(id)]
end

function ClassRef:hasLastReward()
	if self.lastSeasonInfo then
		return self.lastSeasonInfo:hasSeasonReward()
	else
		return false
	end
end

-- 打开宝箱，星星数-10
function ClassRef:openBox()
	self.starCount = UD:getArenaStarCount()
end

function ClassRef:clearLastSeasonReward()
	if self.lastSeasonInfo then
		self.lastSeasonInfo:clearSeasonReward()
	end

	self:clearChangeCache()
end

function ClassRef:refreshStarCount()
	self.starCount = UD:getArenaStarCount()
end

return ClassRef
